package cn.wyu.blogrest.controller;

import cn.wyu.blogrest.entity.Comment;
import cn.wyu.blogrest.entity.Status;
import cn.wyu.blogrest.entity.User;
import cn.wyu.blogrest.repository.AuthRepository;
import cn.wyu.blogrest.repository.CommentRepository;
import cn.wyu.blogrest.repository.StatusRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;

/**
 * Created by ajinlong on 2021/10/26.
 */

@RestController
@RequestMapping(value = "/comment")
@Transactional
public class CommentController {
    @Autowired
    private StatusRepository statusRepository;

    @Autowired
    private CommentRepository commentRepository;

    @Autowired
    private AuthRepository authRepository;

    /**
     * 评论微博
     */

    @RequestMapping(value = "/{statusId}",method = RequestMethod.POST)
    public Comment commentPost(@RequestHeader("accessToken") String accessToken, @PathVariable int statusId,Comment comment){
        if(comment.getContent().length()>0){
            User user = this.authRepository.findFirstByAccessToken(accessToken).getUser();
            Status status = this.statusRepository.findById(statusId);
            comment.setUserName(user.getUserName());
            this.commentRepository.save(comment);
            status.getComments().add(comment);
        }
        return comment;
    }

}
